//
//  CZPingLunHeaderView.swift
//  GZWeibo666
//
//  Created by user on 15/12/2.
//  Copyright © 2015年 itcast. All rights reserved.
//

import UIKit

//代理
protocol CZPingLunHeaderViewDelegate: NSObjectProtocol {
    
    
    func detailHeaderViewClick(headerView: UIView)
}

class CZPingLunHeaderView: UIView {
    
    //代理
    weak var delegate: CZPingLunHeaderViewDelegate?

    
    var status: CZStatus? {
        didSet {
            setButton(commentButton, title: "评论", count: status!.comments_count)
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        //设置背景颜色
        backgroundColor = UIColor(white: 0.99, alpha: 1)
        
        prepareUI()
    }
    
    //设置按钮文字
    private func setButton(button: UIButton, title: String, count: Int){
        if count >= 10000 {
            let final = CGFloat(count) / 10000.0
            var titleText = String(format: "%.1f万", arguments: [final])
            titleText = titleText.stringByReplacingOccurrencesOfString(".0", withString: "")
            
            button.setTitle(title + " " + titleText, forState: UIControlState.Normal)
        }else {
            button.setTitle("\(title)\(count)", forState: UIControlState.Normal)
        }
    }
    
    ///按钮点击事件
    func buttonClick(button: UIButton) {
        delegate?.detailHeaderViewClick(button)
    }
    
    private func prepareUI()
    {
        //添加点击事件
        commentButton.addTarget(self, action: "buttonClick:", forControlEvents: UIControlEvents.TouchUpInside)
        
        //添加子控件
        addSubview(commentButton)
        addSubview(hintView)
        
        //添加约束
        commentButton.ff_AlignInner(type: ff_AlignType.CenterLeft, referView: self, size: CGSize(width: 70, height: 44), offset: CGPoint(x: 10, y: 0))
        
        hintView.translatesAutoresizingMaskIntoConstraints = false
        hintView.ff_AlignInner(type: ff_AlignType.CenterRight, referView: self, size: CGSize(width: 17, height: 11), offset: CGPoint(x: -10, y: 0))
    }
    
    
    // MARK: - 懒加载
    //评论
    
    
    private lazy var commentButton: UIButton = {
        let button = UIButton()
        
        button.setTitle("评论", forState: UIControlState.Normal)
        
        button.titleLabel?.font = UIFont.systemFontOfSize(11)
        
        button.setTitleColor(UIColor.lightGrayColor(), forState: UIControlState.Normal)
        button.setTitleColor(UIColor.darkGrayColor(), forState: UIControlState.Highlighted)
        
        return button
    }()
    
    ///三角型
    private lazy var hintView: UIImageView = {
        let imageView = UIImageView(image: UIImage(named: "statusdetail_segmented_bottom_arrow"))
        
        return imageView
    }()

}
